﻿using System.Collections.Generic;
using System.Linq;
using InterfaceComponents;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Modes
{
    /// <summary>
    /// MenuMode represents the menus of the game. It passes the Draw/Update calls to its components
    /// and checks which component is currently selected.
    /// </summary>
    public class MenuMode : Mode
    {
        private List<InterfaceComponent> components;

        private Texture2D backroundimage
        {
            get;
            set;
        }

        private Color normal
        {
            get;
            set;
        }

        private Color highlight
        {
            get;
            set;
        }

        private int selectedIndex
        {
            get;
            set;
        }

        private MouseState mouse
        {
            get;
            set;
        }

        private MouseState oldMouse
        {
            get;
            set;
        }

        /// <summary>
        /// Draws the backgroundtexture and passes the call to the components.
        /// </summary>
        public override void Draw()
        {
            Rectangle dest = new Rectangle(0, 0, spriteBatch.GraphicsDevice.PresentationParameters.BackBufferWidth, spriteBatch.GraphicsDevice.PresentationParameters.BackBufferHeight);
            spriteBatch.Begin();
            spriteBatch.Draw(backroundimage, dest, Color.White);
            for (int i = 0; i < components.Count; i++)
            {
                if (i == selectedIndex)
                {
                    components.ElementAt(i).Draw(highlight); //highlight the selected component
                }
                else
                {
                    components.ElementAt(i).Draw(normal);
                }
            }
            spriteBatch.End();
        }

        /// <summary>
        /// Passes the Update call to the components and checks the Keystrokes to determine which component is selected.
        /// </summary>
        public override void Update()
        {
            oldKeyboard = keyboard;
            keyboard = Keyboard.GetState();
            oldMouse = mouse;
            mouse = Mouse.GetState();
            if (KeyPressed(Keys.Up))
            {
                do
                {
                    if (selectedIndex == 0)
                    {
                        selectedIndex = components.Count - 1;
                    }
                    else
                    {
                        selectedIndex--;
                    }
                } while (!(components.ElementAt(selectedIndex) is MenuComponent));
            }
            else if (KeyPressed(Keys.Down))
            {
                do
                {
                    if (selectedIndex == components.Count - 1)
                    {
                        selectedIndex = 0;
                    }
                    else
                    {
                        selectedIndex++;
                    }
                } while (!(components.ElementAt(selectedIndex) is MenuComponent));
            }
            foreach (InterfaceComponent interfacecomponent in components)
            {
                if (interfacecomponent is MenuComponent)
                {
                    MenuComponent component = (MenuComponent)interfacecomponent;
                    if (component.Equals(components.ElementAt(selectedIndex)))
                    {
                        component.Update(keyboard, oldKeyboard, mouse, oldMouse, true);
                    }
                    else
                    {
                        component.Update(keyboard, oldKeyboard, mouse, oldMouse, false);
                    }
                }
            }
        }

        public MenuMode(Texture2D backroundimage, List<InterfaceComponent> components, Color highlight, Color normal, SpriteBatch spriteBatch)
        {
            this.backroundimage = backroundimage;
            this.components = components;
            this.highlight = highlight;
            this.normal = normal;
            this.spriteBatch = spriteBatch;
            selectedIndex = 0;
            //Ensures that a selectable component is selected at startup
            while (!(components.ElementAt(selectedIndex) is MenuComponent))
            {
                selectedIndex++;
            }
        }

        private bool KeyPressed(Keys key)
        {
            return oldKeyboard.IsKeyDown(key) && keyboard.IsKeyUp(key);
        }

    }
}

